using System;
using System.Globalization;
using Sedge.Core.FileSystemWrap;
using Sedge.Core.Utils;

namespace Sedge.Core.Globalization
{
	public static class Locale
	{
		private static readonly object _mLockObject = new object();

		private static readonly string _resourcesFolder = String.Empty;
		private static readonly string _resourcesFallbackFolder = String.Empty;
		private static readonly string _resourcesTemplateFolder = String.Empty;

		private static Translation _translation;
		private static TemplateTranslation _templateTranslation;
		private const string BannerImage = "banner.png";

		public static string TemplateFolder
		{
			get { return _resourcesTemplateFolder; }
		}

		static Locale()
		{
			_resourcesFolder = Services.FileSystem.Path.Combine(Services.StartupFolder, "Resources");
			if (!Services.FileSystem.Directory.Exists(_resourcesFolder))
			{
				try
				{
					_resourcesFolder = String.Empty;
					IDirectoryInfoWrap dirInfo = Services.FileSystem.Directory.GetParent(Services.StartupFolder);
					if (dirInfo != null && dirInfo.Parent != null)
					{
						_resourcesFolder = Services.FileSystem.Path.Combine(dirInfo.Parent.FullName, "Resources");
						if (!Services.FileSystem.Directory.Exists(_resourcesFolder))
						{
							_resourcesFolder = String.Empty;
						}
					}
				}
				catch
				{
					_resourcesFolder = String.Empty;
				}
			}

			if (String.IsNullOrEmpty(_resourcesFolder))
			{
				ErrorHandler.Error("Resources not found");
			}
			else
			{
				_resourcesTemplateFolder = Services.FileSystem.Path.Combine(_resourcesFolder, "Template");
				_resourcesFallbackFolder = Services.FileSystem.Path.Combine(_resourcesFolder, "en");
				_resourcesFolder = SearchLocale(_resourcesFolder);
			}
		}

		private static string SearchLocale(string folder)
		{
			string path = Services.FileSystem.Path.Combine(folder, CultureInfo.CurrentCulture.TwoLetterISOLanguageName);
			if (!Services.FileSystem.Directory.Exists(path))
				path = Services.FileSystem.Path.Combine(folder, "en");

			return Services.FileSystem.Directory.Exists(path) ? path : String.Empty;
		}

		public static Translation Strings
		{
			get
			{
				if (null == _translation)
				{
					lock (_mLockObject)
					{
						if (null == _translation)
						{
							_translation = Load<Translation>(GetPath("lang.xml")) ?? new Translation();
						}
					}
				}
				return _translation;
			}
		}

		public static TemplateTranslation TemplateStrings
		{
			get
			{
				if (null == _templateTranslation)
				{
					lock (_mLockObject)
					{
						if (null == _templateTranslation)
						{
							_templateTranslation =
								Load<TemplateTranslation>(Services.FileSystem.Path.Combine(_resourcesTemplateFolder, "lang.xml")) 
									?? new TemplateTranslation();
						}
					}
				}
				return _templateTranslation;
			}
		}

		public static string Banner
		{
			get
			{
				return GetPath(BannerImage);
			}
		}

		private static string GetPath(string fileName)
		{
			string path = Services.FileSystem.Path.Combine(_resourcesFolder, fileName);
			if (!Services.FileSystem.File.Exists(path))
				path = Services.FileSystem.Path.Combine(_resourcesFallbackFolder, fileName);
			
			return path;
		}

		private static T Load<T>(string path)
		{
			return (T)Services.FileSystem.XmlFileSerializer.Load(path, typeof(T));
		}

		public static void SaveCurrent(string path)
		{
			Services.FileSystem.XmlFileSerializer.Save(path, new Translation());
		}

		public static void SaveCurrentTemplate(string path)
		{
			Services.FileSystem.XmlFileSerializer.Save(path, new TemplateTranslation());
		}
	}
}